/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vng.zing.survey.controller;

import com.vng.zing.survey.model.CommonModel;
import com.vng.zing.survey.thrift.client.SurveyClient;
import com.vng.zing.survey.thrift.model.AnswerItem;
import com.vng.zing.survey.thrift.model.UserType;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.log4j.Logger;

/**
 *
 * @author namhh
 */
public class AjaxGetReponseDetail extends BaseController{
    
    private static UserType userType = UserType.USER;

    @Override
    protected String render(HttpServletRequest request, HttpServletResponse resp) throws IOException, Exception {
        try {
            if (request.getParameter("data") == null) {
                return CommonModel.toJSON("ERROR", "Tham số không hợp lệ!");
            }
            String data = request.getParameter("data");
            String[] split = data.split("-");


            Map map = checkMember(request, userType);
            if (map.containsValue(BaseController.ErrorType.FAIL) || map.containsValue(BaseController.ErrorType.ERROR_SYSTEM)) {
                return CommonModel.toJSON("ERROR", (String) map.get("msg"));
            }
            int idUser = Integer.parseInt(split[1]);
            int idSurvey = Integer.parseInt(split[0]);
            String item =  SurveyClient.getInstance().getResponse(idSurvey, idUser);
            item=StringEscapeUtils.unescapeCsv(item);
            if(item!=null){
                return CommonModel.toJSON("SUCCESS",item);
            }
            else{
                return CommonModel.toJSON("ERROR", "");
            }
        } catch (Exception ex) {
            Logger.getLogger(this.getName()).error(ex.toString());
            return CommonModel.toJSON("ERROR", "");
        }
    }
}
